﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace FreeGoo.WpfRenderer.MouseCursor.WpfMouseCursorItems
{
    public abstract class WpfMouseCursorItem
    {
        protected readonly WpfMouseCursor WpfMouseCursor;
        private Point _position;
        private Point _drawnPosition = new Point();

        public WpfMouseCursorItem(WpfMouseCursor wpfMouseCursor, int pathStep)
        {
            WpfMouseCursor = wpfMouseCursor;
            PathStep = pathStep;
            Elements = new List<FrameworkElement>();
            _position = new Point(0, 0);
            BuildGraphics();
        }

        public Point Position
        {
            get
            {
                return _position;
            }

            set
            {
                _position.X = value.X;
                _position.Y = value.Y;
            }
        }

        protected IList<FrameworkElement> Elements { get; private set; }
        protected int PathStep { get; set; }

        public void ClearGraphics()
        {
            foreach (FrameworkElement element in Elements)
            {
                WpfMouseCursor.RenderTarget.Children.Remove(element);
            }

            Elements.Clear();
        }

        public void Draw()
        {
            if (_position.X != _drawnPosition.X || _position.Y != _drawnPosition.Y)
            {
                foreach (FrameworkElement element in Elements)
                {
                    element.SetValue(Canvas.LeftProperty, Position.X);
                    element.SetValue(Canvas.TopProperty, Position.Y);
                }

                _drawnPosition.X = _position.X;
                _drawnPosition.Y = _position.Y;
            }
        }

        protected abstract void BuildGraphics();

        protected double GetSize()
        {
            double val = (double)(WpfMouseCursor.PathSteps - PathStep - 1) / WpfMouseCursor.PathSteps;
            val = val * val;
            return val * WpfMouseCursor.MaxSize + (1 - val) * WpfMouseCursor.MinSize;
        }
    }
}